//
//  TCSGePubCreator.m
//  iPing
//
//  Created by Thomas on 8/19/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "TCSGePubCreator.h"
#import "TCSGStory.h"

#define CREATIONTYPE	@"CreationType"
#define SINGLESTORYMODE	@"SingleStory"
#define PATHVALUE		@"pathValue"
@interface TCSGePubCreator (private)
-(void)createTitlePage;
-(void)createSummaryPageWithID:(int)ID andStory:(TCSGStory *)story;

@end

@implementation TCSGePubCreator
@synthesize currentStories;
@synthesize creationMode;

- (void)setManagedObjectContext:(NSManagedObjectContext *)moc
{
	[moc retain];
	[managedObjectContext release];
	managedObjectContext = moc;
}

+(TCSGePubCreator *)createePubCompilationFromCode:(NSString *)HTMLcode withContext:context;
{
	TCSGePubCreator *creation = [[TCSGePubCreator alloc] init];
	[creation setManagedObjectContext:context];
	creation.currentStories = [NSArray arrayWithObject:[TCSGStory storyForHTMLCode:HTMLcode withContext:context]];
	creation.creationMode = [NSMutableDictionary dictionaryWithObject:SINGLESTORYMODE forKey:CREATIONTYPE];
	NSFileManager *nf = [NSFileManager defaultManager];
	NSString *tempFolder = [[TCSGGeneralMethods tempFolder] stringByAppendingPathComponent:HTMLcode];
	if(![nf fileExistsAtPath:tempFolder])
	{
		[nf removeFileAtPath:tempFolder handler:nil];
	}
	[nf createDirectoryAtPath:tempFolder attributes:nil];
	NSMutableDictionary *hello = [creation.creationMode mutableCopy];
	[hello setObject:tempFolder forKey:PATHVALUE];
	creation.creationMode = hello;
	[hello release];
	//[creation.creationMode setObject:tempFolder forKey:PATHVALUE];
	NSLog(@"%@",creation.creationMode);
	//writing MIMETYPE
	[@"application/epub+zip" writeToFile:[tempFolder stringByAppendingPathComponent:@"mimetype"] atomically:YES encoding:NSUTF8StringEncoding error:nil];
	return creation;
}
-(void)createePub
{
	[self createTitlePage];
	//[self createSummaryPage];
}
-(void)createTitlePage
{
	if([[self.creationMode objectForKey:CREATIONTYPE] isEqualToString:SINGLESTORYMODE])
	{
		TCSGStory *story = [self.currentStories objectAtIndex:0];
		//NSImage *logo = [NSImage imageNamed:story.host];
		NSString *start = [NSString stringWithFormat:@"<?xml version='1.0' encoding='utf-8'?>\n<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">\n<head>\n<title>%@</title>\n<style type=\"text/css\">\nbody {\ntext-align: center;\nvertical-align: center;\noverflow: hidden;\nfont-size: 16pt;\n}\n.logo {\nwidth: 510px; height: 390px;\ntext-align:center;\nfont-size: 1pt;\noverflow:hidden;\n}\nh1 { font-family: serif; }\nh2, h4 { font-family: monospace; }\n</style>\n</head>\n<body>\n<h1>%@</h1>\n<div style=\"text-align:center\">\n<img class=\"logo\" src=\"%@.png\" alt=\"%@ logo\"/>\n</div>\n<h2>%@</h2>\n<h4>Produced by StoryGrabber</h4>\n</body>\n</html>",story.title,story.title,story.host,story.host,story.author,nil];
		[start writeToFile:[[self.creationMode objectForKey:PATHVALUE] stringByAppendingPathComponent:@"sg-titlepage.xhtml"] atomically:YES encoding:NSUTF8StringEncoding error:nil];
		//[self createSummaryPageWithID:1 andStory:story];
		[self saveChaptersForStory:story startingWithID:2];
		NSLog(@"start: %@",start);
	}
}
/*-(void)createSummaryPageWithID:(int)ID andStory:(TCSGStory *)story

{
	NSString *start = [NSString stringWithFormat:@"<?xml version='1.0' encoding='utf-8'?>\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n<title>%@</title>\n<meta content=\"http://www.w3.org/1999/xhtml; charset=utf-8\" http-equiv=\"Content-Type\"/><link href=\"stylesheet.css\" type=\"text/css\" rel=\"stylesheet\"/><style type=\"text/css\">@page { margin-bottom: 5.000000pt; margin-top: 5.000000pt; }</style></head>\n<body class=\"tcsg\">",story.title];
	/*tempTOC = [NSString stringWithFormat:@"<h2 class=\"chapter\" id = \"tcsg_toc_%i\"><a href=PLACEHOLDERTCSGSTRING>%@</a></h2>
			   </h2>\n<br>\n<h3>Author:</h3>%@<br><h3>Synopsis:</h3>%@<br>\n<h3>Codes:</h3>%@<br>",
			   ID,
			   story.title,
			   story.summary,
			   story.author,
			   story.codes,
			   nil];
	NSString *title = [NSString stringWithFormat:@"<h2 class=\"chapter\" id=\"calibre_toc_2\"><a href=\"PLACEHOLDERSTRINGTCSG\">%@</a></h2><p class=\"tcsg3\" style=\"margin:0pt; border:0pt; height:1em\"> </p>",story.title];
	NSString *author = [NSString stringWithFormat:@"<h3 class=\"tcsg4\">Author:</h3>%@<p class=\"tcsg3\" style=\"margin:0pt; border:0pt; height:1em\"> </p>",story.author];
	NSString *summary = [NSString stringWithFormat:@"<h3 class=\"tcsg4\">Synopsis:</h3>%@<p class=\"tcsg3\" style=\"margin:0pt; border:0pt; height:1em\"> </p>",story.summary];
	NSString *codes	= [NSString stringWithFormat:@"<h3 class=\"tcsg4\">Codes:</h3>%@<p class=\"tcsg3\" style=\"margin:0pt; border:0pt; height:1em\"> </p>",story.codes];
	NSString *end = [NSString stringWithFormat:@"<div class=\"tcsg2\" id=\"calibre_pb_3\"/>",nil];
	NSString *tempTOC = [NSString stringWithFormat:@"%@\n%@\n%@\n%@\n%@\n%@\n</body></html>",start,title,author,summary,codes,end,nil];
	[tempTOC writeToFile:[[self.creationMode objectForKey:PATHVALUE] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-split%i.html",story.title,ID,nil]] atomically:YES encoding:NSUTF8StringEncoding error:nil];
	
}*/
-(void)saveChaptersForStory:(TCSGStory *)story startingWithID:(int)ID
{
	NSArray *array = [NSArray arrayWithContentsOfFile:[TCSGGeneralMethods plistPathForHTMLCode:story.HTMLCode]];
	NSString *storyText;
	int i;
	NSString *uuid = story.HTMLCode;

	NSMutableString *string = [NSMutableString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<ncx xmlns=\"http://www.daisy.org/z3986/2005/ncx/\" version=\"2005-1\">\n<head>\n<meta name=\"dtb:uid\" content=\"%@\"/>\n<meta name=\"dtb:depth\" content=\"1\"/>\n<meta name=\"dtb:totalPageCount\" content=\"0\"/>\n<meta name=\"dtb:maxPageNumber\" content=\"0\"/>\n</head>\n<docTitle>\n<text>%@</text>\n</docTitle>\n<navMap>\n",uuid,nil];

	for(i=0;i<[array count];i++)
	{
		storyText = [array objectAtIndex:i];
		storyText = [storyText stringByReplacingOccurrencesOfString:@"<p>" withString:@"<p class=\"tcsg5\">"];
		NSString *header = [NSString stringWithFormat:@"<?xml version='1.0' encoding='utf-8'?>\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n<title>Unknown</title>\n<meta content=\"http://www.w3.org/1999/xhtml; charset=utf-8\" http-equiv=\"Content-Type\"/><link href=\"stylesheet.css\" type=\"text/css\" rel=\"stylesheet\"/><style type=\"text/css\">@page { margin-bottom: 5.000000pt; margin-top: 5.000000pt; }</style></head>\n<body class=\"tcsg\">\n<h3 class=\"tcsg4\">Chapter %i</h3>",(i+1),nil];
		storyText = [NSString stringWithFormat:@"%@\n%@\n</body></html>",header,storyText,nil];
		[storyText writeToFile:[[self.creationMode objectForKey:PATHVALUE] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-split%i.html",story.title,(i+ID),nil]] atomically:YES encoding:NSUTF8StringEncoding error:nil];
		
		[string appendFormat:@"<navPoint id=\"chapter%i\" playOrder=\"%i\">\n<navLabel>\n<text>Chapter %i</text>\n</navLabel>\n<content src=\"%@-split%i.html\"/>\n</navPoint>\n",
		 (i+1),
		 (i+1),
		 (i+1),
		 story.title,
		 (i+ID),
		 nil];
	}
	[string appendString:@"</navMap>\n</ncx>"];
	[string writeToFile:[[self.creationMode objectForKey:PATHVALUE] stringByAppendingPathComponent:@"toc.ncx"] atomically:YES encoding:NSUTF8StringEncoding error:nil];

	//[data writeToFile:@"/Users/tom/Desktop/1.xml" options:NSAtomicWrite error:nil];
}

@end
